/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：BindingCharactor.java <br>
 * 创建时间：2014-8-3 下午5:57:34 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.community;

import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.data.MainChar;

/**
 * 类名：BindingCharactor <br>
 * 说明：社区帐号绑定角色
 *
 * @author 12548
 */
@SuppressWarnings("serial")
public class BindingCharactor extends BmobObject {

	private MainChar charactor;

	/**
	 * @return charactor
	 */
	public MainChar getCharactor() {
		return charactor;
	}

	/**
	 * @param charactor 要设置的 charactor
	 */
	public void setCharactor(MainChar charactor) {
		this.charactor = charactor;
	}


	public void newOrUpdate(final UpdateListener listener) {
		BmobQuery<BindingCharactor> q = new BmobQuery<>();
		q.addWhereEqualTo("owner", GmudUser.getInstance());
		q.findObjects(Game.getInstance(), new FindListener<BindingCharactor>() {

			@Override
			public void onError(int arg0, String arg1) {
				listener.onFailure(arg0, arg1);
			}

			@Override
			public void onSuccess(List<BindingCharactor> arg0) {
				if (arg0.size() == 0) {
					BindingCharactor.this.save(Game.getInstance(), new SaveListener() {

						@Override
						public void onFailure(int arg0, String arg1) {
							listener.onFailure(arg0, arg1);
						}

						@Override
						public void onSuccess() {
							listener.onSuccess();
						}
					});
				} else {
					BindingCharactor.this.setObjectId(arg0.get(0).getObjectId());
					BindingCharactor.this.update(Game.getInstance(), listener);
				}
			}
		});
	}

	protected GmudUser owner;

	/**
	 * @return owner
	 */
	public GmudUser getOwner() {
		return owner;
	}

	/**
	 * @param owner 要设置的 owner
	 */
	public void setOwner(GmudUser owner) {
		this.owner = owner;
	}
}
